//
//  MemberListTableViewCell.m
//  location
//
//  Created by Dongxiang Xu on 2024/4/25.
//

#import "MemberListTableViewCell.h"
#import "ActivityView.h"
#import "UIImageView+WebCache.h"

@interface MemberListTableViewCell ()

@property (nonatomic, strong) UIImageView *avatar;
@property (nonatomic, strong) UIImageView *guView;
@property (nonatomic, strong) UILabel *nickName;
@property (nonatomic, strong) UILabel *lastLog;
@property (nonatomic, strong) UIImageView *activityimg;
@property (nonatomic, strong) ActivityView *arrow;
@property (nonatomic, strong) UILabel *time;

@end

@implementation MemberListTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = RealWhiteColor;
        
        _avatar = [[UIImageView alloc] initWithFrame:CGRectMake(16, 16, 60, 60)];
        _avatar.layer.masksToBounds = YES;
        _avatar.contentMode = UIViewContentModeScaleAspectFill;
        [_avatar.layer setCornerRadius:24];
        _avatar.image = [UIImage imageNamed:@"avatar_gu"];
        [self.contentView addSubview:_avatar];

        _nickName = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_avatar.frame)+16, _avatar.y, SCREEN_WIDTH-CGRectGetMaxX(_avatar.frame)-38, 24)];
        _nickName.textColor = RealBlackColor;
        _nickName.font = MEDIUM_FONT(15);
        [self.contentView addSubview:_nickName];
        
        _activityimg = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_avatar.frame)+16,  CGRectGetMaxY(_nickName.frame)+4, 18, 18)];
        _activityimg.hidden = YES;
        [self.contentView addSubview:_activityimg];
        
        _arrow = [[ActivityView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_activityimg.frame)+2,  CGRectGetMaxY(_nickName.frame)+4, 18, 18)];
        _arrow.hidden = YES;
        [self.contentView addSubview:_arrow];
        
        _lastLog = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_avatar.frame)+16, CGRectGetMaxY(_nickName.frame)+4, SCREEN_WIDTH-CGRectGetMaxX(_avatar.frame)-48, 16)];
        _lastLog.textColor = GraryHeavyLightColor;
        _lastLog.font = DEFAULT_FONT(14);
        _lastLog.text = @"正在获取地址…";
        [self.contentView addSubview:_lastLog];
        
        _time = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_avatar.frame)+16, CGRectGetMaxY(_lastLog.frame)+2, SCREEN_WIDTH-CGRectGetMaxX(_avatar.frame)-48, 16)];
        _time.textColor = GraryTooHeavyColor;
        _time.font = DEFAULT_FONT(14);
        [self.contentView addSubview:_time];
         
        _line = [[UIImageView alloc] initWithFrame:CGRectMake(24, 91, SCREEN_WIDTH-48, 1)];
        _line.image = [UIImage imageNamed:@"line"];
        [self.contentView addSubview:_line];
        
        _guView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_avatar.frame)+16, _avatar.y, SCREEN_WIDTH-CGRectGetMaxX(_avatar.frame)-36, _avatar.height)];
        _guView.image = [UIImage imageNamed:@"member_gu"];
        [self.contentView addSubview:_guView];
    }
    return self;
}

- (void)loadWithInfo:(NSDictionary *)info
{
    if ([info isKindOfClass:[NSDictionary class]]) {
        _guView.hidden = YES;
        if ([info[@"avatar"] isKindOfClass:[NSString class]] && [info[@"avatar"] length]>0) {
            [_avatar sd_setImageWithURL:[NSURL URLWithString:info[@"avatar"]]];
        }else{
            [_avatar sd_setImageWithURL:[NSURL URLWithString:@""]];
        }
        if ([info[@"remark"] isKindOfClass:[NSString class]] && [info[@"remark"] length]>0) {
            _nickName.text = [NSString stringWithFormat:@"%@",info[@"remark"]];
        }else{
            if ([info[@"nickName"] isKindOfClass:[NSString class]] && [info[@"nickName"] length]>0) {
                _nickName.text = [NSString stringWithFormat:@"%@",info[@"nickName"]];
            }else{
                _nickName.text = @"";
            }
        }
        
        if ([info[@"activity"] isKindOfClass:[NSString class]] &&  [info[@"activity"] containsString:@"sos"]) {
            _nickName.text = [_nickName.text stringByAppendingString:@" 呼救中..."];
            _nickName.textColor = RedColor;
        }else{
            _nickName.textColor = RealBlackColor;
        }
        

        if (![info[@"isLocationAuth"] isKindOfClass:[NSNull class]] && info[@"isLocationAuth"]) {
            if ([info[@"isLocationAuth"] integerValue]>0) {
                if ((!info[@"latitude"] && !info[@"longtitude"]) || ([info[@"latitude"] doubleValue]>0 && [info[@"longtitude"] doubleValue]>0)) {
                    if ([info[@"logout"] isKindOfClass:[NSString class]] && [info[@"logout"] integerValue]>0 && [info[@"userId"] integerValue]!=[XDXShareInstance shareInstance].userId.integerValue) {
                        _lastLog.textColor = RedColor;
                        _lastLog.text = @"网络异常或手机关机";
                    }else{
                        _lastLog.textColor = GraryHeavyLightColor;
                        if ([info[@"lastlog"] isKindOfClass:[NSString class]] && [info[@"lastlog"] length]>0) {
                            _lastLog.text = [NSString stringWithFormat:@"%@",info[@"lastlog"]];
                        }else{
                            _lastLog.text = @"正在获取地址…";
                        }
                    }
                }else{
                    _lastLog.textColor = RedColor;
                    _lastLog.text = @"GPS定位信号弱";
                }
            } else {
                _lastLog.textColor = RedColor;
                _lastLog.text = @"定位权限未开启";
            }
        }else{
            _lastLog.textColor = GraryHeavyLightColor;
            _lastLog.text = @"正在获取地址…";
        }
        
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        dateFormatter.locale = [NSLocale systemLocale];
        dateFormatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
        NSDate *firstDate = [dateFormatter dateFromString:[[info[@"timestamp"] substringToIndex:19] stringByReplacingOccurrencesOfString:@"T" withString:@" "]];
        NSInteger firstSecond = [firstDate timeIntervalSince1970];
        NSInteger lastSecond = [[XDXTimeTool getNowTimeTimestamp] integerValue];
        NSInteger second = lastSecond-firstSecond;
        
        // 最后一次运动状态超过90s，则视为静止
        if (([info[@"activity"] containsString:@"静止"] || second>ABNORMAL_DURATION || !info[@"activity"] || [info[@"activity"] isKindOfClass:[NSNull class]] ||  [_lastLog.text containsString:@"网络异常"] || [_lastLog.text containsString:@"权限未开启"] || [info[@"activity"] containsString:@"sos"])) {
            _activityimg.hidden = YES;
            _arrow.hidden = YES;
            if (![info[@"timestamp"] isKindOfClass:[NSNull class]] && [info[@"timestamp"] length]>0 && [info[@"activity"] isKindOfClass:[NSString class]]) {
                _time.text = [XDXTimeTool compareCurrentTimeWithDate:[info[@"timestamp"] substringToIndex:19]];
            }else{
                _time.text = @"";
            }
           
            _nickName.y = _avatar.y;
            [_lastLog setFrame:CGRectMake(CGRectGetMaxX(_avatar.frame)+16, CGRectGetMaxY(_nickName.frame)+4, SCREEN_WIDTH-CGRectGetMaxX(_avatar.frame)-48, 16)];
        }else{
            _activityimg.hidden = NO;
            _arrow.hidden = NO;
          
            if ([info[@"activity"] isKindOfClass:[NSString class]] && [info[@"activity"] containsString:@"驾车"]) {
                _activityimg.image = [UIImage imageNamed:@"drive"];
            }else {
                _activityimg.image = [UIImage imageNamed:@"walk"];
            }
            _time.text = @"";
            _nickName.y = 23;
            [_activityimg setFrame:CGRectMake(CGRectGetMaxX(_avatar.frame)+16,  CGRectGetMaxY(_nickName.frame)+4, 18, 18)];
            [_arrow setFrame:CGRectMake(CGRectGetMaxX(_activityimg.frame)+2,  CGRectGetMaxY(_nickName.frame)+4, 18, 18)];
            [_lastLog setFrame:CGRectMake(CGRectGetMaxX(_arrow.frame)+2, CGRectGetMaxY(_nickName.frame)+4, SCREEN_WIDTH-CGRectGetMaxX(_avatar.frame)-48, 16)];
        }
    }else{
        _activityimg.hidden = YES;
        _arrow.hidden = YES;
        [_avatar sd_setImageWithURL:[NSURL URLWithString:@""]];
        _nickName.text = @"";
        _lastLog.text = @"正在获取地址…";
        _time.text = @"";
    }
}

@end
